//
//  MyViewController.m
//  WhatToEat
//
//  Created by Mac on 15/3/18.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "MyViewController.h"
#import "MyCell.h"
#import "EAHelpController.h"
#import "EAAboutUsViewController.h"
#import "ConstansCode.h"
#import <objc/runtime.h>
@interface MyViewController ()

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSArray * dataSource;
@property (strong,nonatomic)UIImage *headImage;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self updateUI];
    
    

}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    
        self.navigationController.navigationBar.hidden=YES;
    

}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    

}


-(void)updateUI
{
    self.view.backgroundColor=kWhiteColor;
    
   
    self.bgView.backgroundColor=kTingColor;
    
    self.myImageView.layer.cornerRadius=CGRectGetMidX(self.myImageView.bounds);
    self.myImageView.clipsToBounds=YES;
    self.myImageView.layer.borderColor=[UIColor colorWithRed:255/225.0 green:165/255.0 blue:91/255.0 alpha:1].CGColor;
    self.myImageView.layer.borderWidth=2;
    
    

    self.tableView.backgroundColor=kWhiteColor;

    [self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"MyCell"];

    
    self.dataSource=@[@"个人信息",@"体重分析",@"饮食分析",@"退出登录"];
    
    self.nameLabel.text=ApplicationDelegate.userDictionary[@"username"];
    

    [self.headImageView setImageWithURL:[NSURL URLWithString:ApplicationDelegate.headImage]];
    
  

    
    
    self.changeBtn.clipsToBounds=YES;
    
    self.changeBtn.layer.cornerRadius=kRadius;

    UITapGestureRecognizer *tp=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sendHeaderImage)];
    
    
    self.headImageView.userInteractionEnabled=YES;
    [self.headImageView addGestureRecognizer:tp];
    
    


self.tableView.separatorStyle = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor=[UIColor lightGrayColor];
    
    
    [Commtools setExtraCellLineHidden:self.tableView];
}



- (UIActionSheet *)photoSheet
{
    if (!_photoSheet) {
        _photoSheet = [[UIActionSheet alloc] initWithTitle:@"上传头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选取",@"即刻拍照", nil];
        _photoSheet.delegate = self;
    }
    return _photoSheet;
}

-(void)sendHeaderImage
{
    
    
    
    
    [[self photoSheet] showInView:self.view];
  


}



- (UIImagePickerController *)imagePickerController
{
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.mediaTypes = @[(__bridge NSString *)kUTTypeImage];
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
            
        } else {
            /* Configure Appearence for iOS 7 only */
            _imagePickerController.navigationBar.barTintColor = nil;
            _imagePickerController.navigationBar.titleTextAttributes = nil;
            _imagePickerController.navigationBar.tintColor = kTingColor;
            _imagePickerController.navigationBar.backIndicatorImage = nil;
            _imagePickerController.navigationBar.backIndicatorTransitionMaskImage = nil;
        }
        _imagePickerController.delegate = self;
    }
    return _imagePickerController;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == actionSheet.firstOtherButtonIndex) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
    } else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) {
//        if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
//            [self.tableView clearSelection];
//            return;
//        }
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePickerController.allowsEditing = YES;
        _imagePickerController.showsCameraControls = YES;
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
    }
}
-(void)setHeadImage:(UIImage *)headImage
{
    _headImage=headImage;
    

    
    
    
    NSDictionary *d=@{
                      @"time":[CurrentDate timgString],
                      
                      @"sign":[WebAPI getSignString],
                      
                      @"userID": ApplicationDelegate.userId
                      
                      
                      
                      
                      };
    
    
    
    [[WebAPI defaultManager] updateAimg:headImage andView:self.view andisHeadPic:NO andParams:d andUrlString:kImgUrl andComplete:^(NSInteger state, id responseObject, NSString *filename) {
        
        
        
        
          self.headImageView.image=headImage; 
                                                                                                     
                                                                                                     
                                                                                                     
                                                                                                 }];
    
    



}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    UIButton *custom = [[UIButton alloc] initWithFrame:CGRectMake(0,0,60,30)];
    
    [custom setTitle:@"取消" forState:0];
    
//    navigationController.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:0 target:self action:@selector(hidePhoto)];
  
    navigationController.navigationBar.tintColor=[UIColor whiteColor];
    
    [custom addTarget:self action:@selector(hidePhoto) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom];
    [viewController.navigationItem setRightBarButtonItem:btn animated:NO];
    
}

-(void)hidePhoto
{
[_imagePickerController dismissViewControllerAnimated:YES completion:^{
    
}];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    if (info[UIImagePickerControllerMediaType] == (__bridge NSString *)kUTTypeImage) {
        
        self.headImage = info[@"UIImagePickerControllerOriginalImage"];
        
        
        

        
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
     [self dismissViewControllerAnimated:YES completion:nil];

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataSource.count;


}


- (void)setExtraCellLineHidden: (UITableView *)tableView{
    
    UIView *view =[ [UIView alloc]init];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
    [tableView setTableHeaderView:view];
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    cell.label.text=self.dataSource[indexPath.row];
    
    cell.heardImage.image=[UIImage imageNamed:[NSString stringWithFormat:@"personalCenter0%ld",indexPath.row+1]];

    cell.label.textColor=[UIColor lightGrayColor];
    
    
//    UIView *v=[[UIView alloc] init];
//    v.backgroundColor=[UIColor clearColor];
//    cell.selectedBackgroundView=v;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;

}

- (IBAction)backAcion:(UIButton *)sender
{
    [ApplicationDelegate.ddVc showRootController:YES];
}

- (IBAction)exitOut:(id)sender
{
    

    
    
    [UIView transitionWithView:self.view.window
                      duration:0.5
                       options: UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{
                        ApplicationDelegate.window.rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"logoInroot"];
                    }
                    completion:nil];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    switch (indexPath.row) {
        case 0:
        {
            
 
            CompleteTableController *cVC=[self.storyboard instantiateViewControllerWithIdentifier:@"CompleteTableController"];
            
            
    
            
            cVC.isInformation=YES;
            
            [self.navigationController pushViewController:cVC animated:YES];
        
        
        }
            
            break;
        case 1:
        
        {
            
            EAHeightAndWeightAnalyzeController * eVC=[[EAHeightAndWeightAnalyzeController alloc] init];
              eVC.type=@"heigt";
           
            self.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:eVC animated:YES];
        
            self.hidesBottomBarWhenPushed=NO;
            
        
        }
            break;
        case 2:
        {
        
            EAHeightAndWeightAnalyzeController * eVC=[[EAHeightAndWeightAnalyzeController alloc] init];
            
            
             eVC.type=@"weight";
            
            
            
            self.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:eVC animated:YES];
        
            self.hidesBottomBarWhenPushed=NO;
        
        
        
        }
            
            
            
            break;
        case 3:
            
            [self exitOut:nil];
            
            break;
            case 4:
        {
            
            EAFavoritesController *fvc=[self.storyboard instantiateViewControllerWithIdentifier:@"EAOpinionViewController"];
            
            
            UITabBarController *vc=(UITabBarController *)ApplicationDelegate.ddVc.rootViewController;
            

            
            
            [vc.viewControllers[0] pushViewController:fvc
                                             animated:YES];
            
            
       
            
            
            
        }
            break;
            
        case 5:
            
        {
        
            EAHelpController *fvc=[self.storyboard instantiateViewControllerWithIdentifier:@"EAHelpController"];
            
            
            UITabBarController *vc=(UITabBarController *)ApplicationDelegate.ddVc.rootViewController;
            
 

            [vc.viewControllers[0] pushViewController:fvc animated:YES];
        
        
        
        
        }
            
            
            break;
            
        case 6:
            
            
            
             [MobClick checkUpdate];
             [SVProgressHUD showImage:nil status:@"目前已经是最新版本"];

            
            break;
         
        case 7:
        {
            
            EAFavoritesController *fvc=[self.storyboard instantiateViewControllerWithIdentifier:@"EAAboutUsViewController"];
            
            
            UITabBarController *vc=(UITabBarController *)ApplicationDelegate.ddVc.rootViewController;
            
            
            
            
            
            [vc.viewControllers[0] pushViewController:fvc animated:YES];
            
            
            
            
        }
            
            
        default:
            break;
    }

//    [tableView reloadData];

}



#pragma mark-----分享视图
-(void)showAction
{
    NSLog(@"我是分享");
    
    NSArray *shareButtonTitleArray = [[NSArray alloc] init];
    NSArray *shareButtonImageNameArray = [[NSArray alloc] init];
    

    shareButtonImageNameArray= @[@"wx",@"pyq",@"sjqq",@"wb",@"qqkj",@"dx",@"fzlj"];
    
    
    LXActivity *lxActivity = [[LXActivity alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" ShareButtonTitles:shareButtonTitleArray withShareButtonImagesName:shareButtonImageNameArray];
    
    
    [lxActivity showInView:ApplicationDelegate.window];
    [ApplicationDelegate.window insertSubview:lxActivity aboveSubview:self.inputView];
}

- (void)didClickOnImageIndex:(NSInteger )imageIndex
{

    NSString *UMShareSns;
    
    
    switch (imageIndex) {
        case 0:
            
            UMShareSns=UMShareToWechatSession;
            
            
            break;
            
            
        case 1:
            
            UMShareSns=UMShareToWechatTimeline;
            
            
            break;
        case 2:
            
            UMShareSns=UMShareToQQ;
            
            
            break;
        case 3:
            
            UMShareSns=UMShareToSina;
            
            
            break;
        case 4:
            
            UMShareSns=UMShareToQzone;
            
            
            break;
        case 5:
            
            UMShareSns=UMShareToSms;
            
            
            break;
        case 6:
        
            {
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                pasteboard.string =[NSString stringWithFormat:@"%@\n%@",shareContent,shareUrl];
                
                
                
                [SVProgressHUD showImage:nil status:@"网址已经复制，您可以直接发送给好友"];
                
                      return;
            }
        
      
        
        
    
            
            
            break;

            
        default:
            break;
    }
    
    
    
    
    
    [self yixunshare:UMShareSns];


}

-(void)yixunshare:(NSString *)pingtai
{
    

    
    
    NSString *shareText = shareContent;
    

    
    
    
    UMSocialUrlResource *UMUrl=[[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:shareUrl];
    
    if ([pingtai isEqualToString:UMShareToSina]) {
        shareText = [NSString stringWithFormat:@"%@%@",shareTitle,shareContent];
        
    }
    
    
    
    
    [[UMSocialDataService defaultDataService] postSNSWithTypes:@[pingtai] content:shareContent image:shareImage location:nil urlResource:UMUrl presentedController:self completion:^(UMSocialResponseEntity * response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:@"分享成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alertView show];
        } else if(response.responseCode != UMSResponseCodeCancel) {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"失败" message:@"分享失败" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alertView show];
        }
    }];
    
}




@end
